package com.ljx.video.util;

import org.springframework.stereotype.Component;

/**
 * @Author: LiJunXi
 * @Description:
 * @Date: 2025-02-03  10:45
 */
@Component
public class FfmpegUtil {

    /**
     * 获取播放时长 E:/ffmpeg/ffprobe.exe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 沉浸式体验爽局，快500分一回合的机械猎手.mp4
     * 获取视频编码 E:/ffmpeg/ffprobe.exe -v error -select_streams v:0 -show_entries stream=codec_name 沉浸式体验爽局，快500分一回合的机械猎手.mp4
     * 视频转码为 h264 E:/ffmpeg/ffprobe.exe -i 沉浸式 体验爽局，快500分一回合的机械猎手.mp4 -c:v libx264 -crf 20 result_沉浸式 体验爽局，快500分一回合的机械猎手.mp4 -y
     * 生成 index.ts 文件 E:/ffmpeg/ffmpeg.exe -y -i 沉浸式 体验爽局，快500分一回合的机械猎手.mp4 -vcodec copy -acodec copy -vbsf h264_mp4toannexb index.ts
     * 将 index.ts 文件切片，并生成 .m3u8 索引 E:/ffmpeg/ffmpeg.exe -i index.ts -c copy -map 0 -f segment -segment_list index.m3u8 -segment_time 10 %4d.ts
     */
    public ProcessBuilder getVideoCodecProcessBuilder(String filePath) {
        return new ProcessBuilder(
                "E:/ffmpeg/ffprobe.exe",
                "-v", "error",
                "-select_streams", "v:0",
                "-show_entries", "stream=codec_name",
                filePath
        );
    }

    public ProcessBuilder convertVideoCodecProcessBuilder(String filePath, String newFilePath) {
        return new ProcessBuilder(
                "E:/ffmpeg/ffprobe.exe",
                "-i", filePath,
                "-c:v", "libx264",
                "-crf", "20",
                "-show_entries", "stream=codec_name",
                newFilePath
        );
    }

    public ProcessBuilder getIndexTsProcessBuilder(String filePath, String newFilePath) {
        return new ProcessBuilder(
                "E:/ffmpeg/ffmpeg.exe",
                "-i", filePath,
                "-vcodec", "copy",
                "-acodec", "copy",
                "-vbsf", "h264_mp4toannexb",
                newFilePath
        );
    }

    public ProcessBuilder cutIndexTsProcessBuilder(String filePath, String m3u8Path, String newFilePath) {
        return new ProcessBuilder(
                "E:/ffmpeg/ffmpeg.exe",
                "-i", filePath,
                "-c", "copy",
                "-map", "0",
                "-f", "segment",
                "-segment_list", m3u8Path,
                "-segment_time", "10",
                newFilePath
        );
    }

}
